package com.gh.rabbitmq.five;

import com.gh.rabbitmq.utils.RabbitmqUtils;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

/**
 * @Author Eric
 * @Date 2021/7/18 14:17
 * @Version 1.0
 * 接收消息
 */
public class ReceiveLog02 {

    public static final String EXCHANGE_NAME="logs";

    public static void main(String[] args) throws Exception{
        Channel channel = RabbitmqUtils.getChannel();
        //声明一个交换机
        channel.exchangeDeclare(EXCHANGE_NAME,"fanout");
        //声明一个临时队列
        String queue = channel.queueDeclare().getQueue();
        //绑定交换机与队列 routingKey为空
        channel.queueBind(queue,EXCHANGE_NAME,"");
        System.out.println("等待接收消息,接收到的消息打印在屏幕上");
        //声明接收消息
        DeliverCallback deliverCallback=(consumerTag, message)->{
            System.out.println("02在控制台接收的消息:"+new String(message.getBody()));
        };
        //取消消息时的回调
        CancelCallback cancelCallback= consumerTag->{
            System.out.println("消费消息被中断了");
        };
        channel.basicConsume(queue,true,deliverCallback,cancelCallback);
    }
}
